package de.jmda.mview.typeshape.relation;

import java.awt.Point;
import java.awt.geom.Line2D;
import java.util.LinkedList;
import java.util.List;

/**
 * <b>R</b>elation <b>E</b>ndpoint <b>C</b>onnection (REC) connecting source
 * with target connector point in <b>O</b>rthogonal routing style. One relation
 * endpoint has <b>H</b>orizontal and the other <b>V</b>ertical direction (H2V).
 *
 * @author roger.jmda@gmail.com
 */
public class RECOH2V
    extends RECOrthogonal
{
	private List<Line2D> lines;

	/**
	 * @param relationEndpointHelperSource
	 * @param relationEndpointHelperTarget
	 */
	public RECOH2V(
      RelationEndpointHelper relationEndpointHelperSource,
      RelationEndpointHelper relationEndpointHelperTarget)
			throws IllegalArgumentException
  {
	  super(relationEndpointHelperSource, relationEndpointHelperTarget);
  }
	
	@Override
	protected void checkStateInvalidated() throws StateInvalidatedException
	{
		super.checkStateInvalidated();

		// check if one relation endpoint has vertical and the other has horizontal
		// direction
	  if (false == checkRelationEndpointsHorizontalAndVertical())
		{
			throw new StateInvalidatedException(
					"both relation endpoints have to be horizontal\n" +
					"source relation endpoint is horizontal: " +
							relationEndpointHelperSource.isHorizontal() + "\n" +
					"target relation endpoint is horizontal: " +
							relationEndpointHelperTarget.isHorizontal());
		}
	}

	private boolean checkRelationEndpointsHorizontalAndVertical()
	{
		return
				RECOH2V.checkRelationEndpointsHorizontalAndVertical(
						relationEndpointHelperSource, relationEndpointHelperTarget);
	}

	public static boolean checkRelationEndpointsHorizontalAndVertical(
			RelationEndpointHelper r1, RelationEndpointHelper r2)
	{
		return
				((r1.isHorizontal() && r2.isVertical()) ||
				 (r1.isVertical()   && r2.isHorizontal()));
	}

	@Override
	protected List<Line2D> getLines()
	{
		if (lines == null)
		{
			lines = new LinkedList<Line2D>();
		}
		return lines;
	}

	@Override
	protected void update() throws StateInvalidatedException
	{
		super.update();
		
		// just shortcuts
		Point sourceConnPoint = relationEndpointHelperSource.getConnectorPoint();
		Point targetConnPoint = relationEndpointHelperTarget.getConnectorPoint();
		
		List<Line2D> lines = getLines();

		lines.clear();
		
		if (relationEndpointHelperSource.isHorizontal())
		{
			// |
			lines.add(
					new Line2D.Double(
							sourceConnPoint.x, sourceConnPoint.y,
							sourceConnPoint.x, targetConnPoint.y));
			// -
			lines.add(
					new Line2D.Double(
							sourceConnPoint.x, targetConnPoint.y,
							targetConnPoint.x, targetConnPoint.y));
		}
		else
		{
			// -
			lines.add(
					new Line2D.Double(
							sourceConnPoint.x, sourceConnPoint.y,
							targetConnPoint.x, sourceConnPoint.y));
			// |
			lines.add(
					new Line2D.Double(
							targetConnPoint.x, sourceConnPoint.y,
							targetConnPoint.x, targetConnPoint.y));
		}
	}
}